from collections import defaultdict
from decorator import decorator
from inspect import formatargspec, getargspec
import logging

from . import pycompat

unsafe_eval = eval

_logger = logging.getLogger(__name__)

class ormcache_counter(object):
    """ Statistic counters for cache entries. """
    __slots__ = ['hit', 'miss', 'err']

    def __init__(self):
        self.hit = 0
        self.miss = 0
        self.err = 0

    @property
    def ratio(self):
        pass

STAT = defaultdict(ormcache_counter)

class ormcache(object):
    def __init__(self, *args, **kwargs):
        self.args = args
        self.skiparg = kwargs.get('skiparg')

    def __call__(self, method):
        self.method = method
        self.determine_key()
        lookup = decorator(self.lookup, method)
        lookup.clear_cache = self.clear
        return lookup

    def determine_key(self):
        if self.skiparg is None:
            # build a string that represents function code and evaluate it
            args = formatargspec(*getargspec(self.method))[1:-1]
            if self.args:
                code = "lambda %s: (%s,)" % (args, ", ".join(self.args))
            else:
                code = "lambda %s: ()" % (args,)
            self.key = unsafe_eval(code)

    def lru(self, model):
        pass

    def lookup(self, method, *args, **kwargs):
        pass

    def clear(self, model, *args):
        pass

class ormcache_context(ormcache):
    def __init__(self, *args, **kwargs):
        pass

    def determine_key(self):
        pass

class ormcache_multi(ormcache):
    def __init__(self, *args, **kwargs):
        pass

    def determine_key(self):
        pass

    def lookup(self, method, *args, **kwargs):
        pass

class dummy_cache(object):
    def __init__(self, *l, **kw):
        pass

    def __call__(self, fn):
        pass

    def clear(self, *l, **kw):
        pass

def log_ormcache_stats(sig=None, frame=None):
    pass

def get_cache_key_counter(bound_method, *args, **kwargs):
    pass

cache = ormcache